#include "cvlist.h"

CVList::CVList(bool showLoader, QWidget *parent) :
    QScrollArea(parent)
{
    this->showLoader = showLoader;
    mainLayout = new QVBoxLayout();
    mainLayout->setObjectName("CVListLayout");
    if (showLoader)
    {
        loader = new QMovie(":/images/loader");
        loaderContainer = new QLabel();
        loaderContainer->setAlignment(Qt::AlignCenter);
        loaderContainer->setMovie(loader);
        mainLayout->addWidget(loaderContainer);
        startLoading();
    }
    innerWidget = new QWidget();
    innerWidget->setObjectName("cvlistWidget");
    innerWidget->setLayout(mainLayout);
    setWidget(innerWidget);
    setWidgetResizable(true);
}
/*
void CVList::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
*/
void CVList::addStretch()
{
    mainLayout->addStretch();
}

void CVList::appendItem(QWidget *widget)
{
    mainLayout->addWidget(widget);
}

void CVList::appendItem(CVActivityWidget *widget)
{
    mainLayout->addWidget(widget);
}

void CVList::addItems(QList<CVActivityWidget *> list)
{
    int size = list.size();
    for (int i = 0; i < size; ++i)
    {
        CVActivityWidget *item = list.at(i);
        appendItem(item);
    }

    repaint();
}

void CVList::setItems(QList<CVActivityWidget *> list)
{
    // delete all original items
    clear();

    addItems(list);
}

void CVList::clear()
{
    if (mainLayout != NULL)
    {
        QLayoutItem *item;
        while ((item = mainLayout->takeAt(0)) != NULL)
        {
            delete item->widget();
            delete item;
        }
    }
}

void CVList::startLoading()
{
    if (this->showLoader)
    {
        loader->start();
    }
}

void CVList::finishLoading()
{
    if (this->showLoader)
    {
        loader->stop();
    }
}

